﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule
{
    public class MobileDeviceInfoManager : IBusinessLogic
    {
        #region IBusinessLogic Members

        object IBusinessLogic.Load(decimal ID)
        {
            return ObjectRow.Load(typeof(MobileDeviceInfo), "id='" + ID + "'");
        }

        object IBusinessLogic.LoadByCode(string IMEICode)
        {
            return ObjectRow.Load(typeof(MobileDeviceInfo), "IMEICode='" + IMEICode + "'");
        }

        object IBusinessLogic.LoadWithCondition(string Condition)
        {
            return new ObjectTable(typeof(MobileDeviceInfo), Condition);
        }

        void IBusinessLogic.Save(object obj)
        {
            throw new NotImplementedException();
        }

        #endregion

        public MobileDeviceInfo LoadByCode(String IMEICode)
        {
            return (MobileDeviceInfo)((IBusinessLogic)this).LoadByCode(IMEICode);
        }
        public MobileDeviceInfo LoadByID(int id)
        {
            return (MobileDeviceInfo)((IBusinessLogic)this).Load(id);
        }

        public ObjectTable LoadWithCondition(String condition)
        {
            return (ObjectTable)((IBusinessLogic)this).LoadWithCondition(condition);
        }

        /// <summary>
        /// 快速检索
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ObjectTable QuickSearch(String condition)
        {
            String sql = " 1=1 ";// " id not in (select DeviceId from UserDeviceBound)";
            if (condition == null)
                return null;
            condition = condition.Replace("'", "");
            if (!condition.Equals(""))
            {
                sql += String.Format("and (IMEICode like '{0}' or BrandName like '{0}' or Model like '{0}')", condition);
            }

            return LoadWithCondition(sql);

        }



        /// <summary>
        /// 快速检索
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public DataTable GetDeviceAndUserInfoByWhere(decimal domainId, String keyWord, String condition)
        {
            String sql = String.Format(@"select m.ID,m.IMEICode,m.BrandName,m.Model,m.ACTIVE as DeviceActive,p.NAME as PersonName,u.CODE as LoginCode,(u.CODE+'/'+p.NAME) as loginName,ud.ACTIVE as BoundActive
                                        from MobileDeviceInfo m
                                        left join UserDeviceBound ud
                                        on ud.DeviceID=m.ID
                                        left  join USERS u
                                        on u.ID=ud.UserID
                                        left join PERSONS p
                                        on p.ID=u.PERSON_ID
                                        where m.DOMAIN_ID={0} and (m.IMEICode like '%{1}%' or m.BrandName like '%{1}%' or m.model like '%{1}%')
                                        {2}", domainId, keyWord, condition);

            return Jumpton.Data.SqlData.OpenSql(sql).Tables[0];

        }

        public DataTable GetDeviceUnboundUsers(decimal domainId)
        {
            String sql = String.Format(@"  select u.ID,u.CODE,(p.NAME+'('+u.CODE)+')' as name from USERS u 
                                        inner join PERSONS p
                                        on u.PERSON_ID=p.ID
                                         where not exists (select 1 from UserDeviceBound where UserDeviceBound.UserID=u.ID and DOMAIN_ID={0})
                                         and u.DOMAIN_ID={0} order by p.NAME ", domainId);
            return Jumpton.Data.SqlData.OpenSql(sql).Tables[0];

        }
    }
}
